#ifndef DIARY_INTERFACE_H
#define DIARY_INTERFACE_H

#include "crow_all.h"
#include "database/database.h"
#include <sqlite3.h>
#include <zlib.h>
#include <string>
#include <vector>
#include <ctime>

struct DiaryEntry {
    int diary_id;
    int user_id;
    std::string username;
    std::string title;
    std::string content;
    std::string destination;
    int views;
    double average_rating;
    std::string created_at;
    std::string updated_at;
};

class DiaryInterface {
public:
    static void setupDiaryRoutes(crow::SimpleApp& app, Database& db);

private:
    // 压缩相关
    static std::string compressContent(const std::string& content);
    static std::string decompressContent(const std::string& compressed);

    // 日记基本操作
    static bool createDiary(sqlite3* db, const std::string& title, const std::string& destination, 
                          const std::string& content, int user_id);
    static std::vector<DiaryEntry> getUserDiaries(sqlite3* db, int user_id);
    static DiaryEntry getDiary(sqlite3* db, int diary_id);
    static bool updateDiary(sqlite3* db, int diary_id, const std::string& title, 
                          const std::string& destination, const std::string& content);
    static bool deleteDiary(sqlite3* db, int diary_id);
    static bool increaseViews(sqlite3* db, int diary_id);

    // 评分相关
    static bool addRating(sqlite3* db, int diary_id, int user_id, double rating);
    static double getAverageRating(sqlite3* db, int diary_id);
    static double getUserRating(sqlite3* db, int diary_id, int user_id);

    // 搜索相关
    static std::vector<DiaryEntry> searchByDestination(sqlite3* db, const std::string& destination, 
                                                     const std::string& sort_by = "heat",
                                                     int page = 1, int pageSize = 9);
    static std::vector<DiaryEntry> fullTextSearch(sqlite3* db, const std::string& query, 
                                                 const std::string& sortBy = "heat",
                                                 int page = 1, int pageSize = 9);
    static std::vector<DiaryEntry> recommendDiaries(sqlite3* db, int user_id, 
                                                  const std::string& interest = "", 
                                                  const std::string& sortBy = "heat",
                                                  int page = 1, int pageSize = 9);
    
    // 获取总数的辅助方法
    static int getTotalDiariesCount(sqlite3* db);
    static int getSearchResultsCount(sqlite3* db, const std::string& query, const std::string& searchType);
    
    // 辅助方法
    static std::string getUsernameById(sqlite3* db, int user_id);
};

#endif // DIARY_INTERFACE_H 